<template>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="名称">
            <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
export default {
    data() {
        var checkAge = (rule, value, callback) => {};
        var validatePass = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请输入密码"));
            } else {
                if (this.ruleForm.checkPass !== "") {
                    this.$refs.ruleForm.validateField("checkPass");
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请再次输入密码"));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error("两次输入密码不一致!"));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                pass: "",
                checkPass: "",
                age: "",
                name: "",
            },
            rules: {
                pass: [{ validator: validatePass, trigger: "blur" }],
                checkPass: [{ validator: validatePass2, trigger: "blur" }],
                age: [{ validator: checkAge, trigger: "blur" }],
            },
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    // console.log(valid);
                    //aaaaaxxxxxiiiiosss
                    let password = this.ruleForm.pass;
                    let userName = this.ruleForm.name;
                    let data = await this.$API.RigesterAPI.GetLogin({
                        password,
                        userName,
                    });
                    if (data.code == 1) {
                        this.open2();
                        let boo = confirm("跳转到登录");
                        if (boo) {
                            this.$router.push("/login");
                        }
                    } else {
                        this.open4();
                    }
                    // console.log(data);
                    // alert("submit!");
                } else {
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.ruleForm.name = "";
        },
        open2() {
            this.$message({
                message: "恭喜你，注册成功,请登录",
                type: "success",
            });
        },
        open4() {
            this.$message.error("注册失败");
        },
    },
};
</script>

<style lang="scss" scoped>
.el-form {
    width: 400px;
    height: 400px;
    position: relative;
    top: 140px;
    left: 35%;
}
</style>